﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeoAPI.CoordinateSystems.Transformations;
using ProjNet.IO.CoordinateSystems;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("lishanyang");
            double[] aa = ProjectedCoordinate(92884.567463,102643.160932);
            Console.WriteLine(aa[0].ToString()+":"+aa[1].ToString());
            String aaa = Console.ReadLine();
        }
        public static double[] ProjectedCoordinate(double x, double y)
        {
            var d = 0.001343;
            var dx = 435542.834434922;
            var dy = 3267268.38850877;
            var k = 0.999991455270843;
            var transCord = ReverseTransfer_4_Parameter(x, y, k, d, dx, dy);
            GeographicCoordinateSystem toCS = CoordinateSystemWktReader.Parse("GEOGCS[\"China Geodetic Coordinate System 2000\",DATUM[\"China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"1024\"]],AUTHORITY[\"EPSG\",\"1043\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4490\"]]") as GeographicCoordinateSystem;
            ProjectedCoordinateSystem fromCS = CoordinateSystemWktReader.Parse("PROJCS[\"CGCS2000 / 3-degree Gauss-Kruger CM 120E\",GEOGCS[\"China Geodetic Coordinate System 2000\",DATUM[\"China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"1024\"]],AUTHORITY[\"EPSG\",\"1043\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4490\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",120],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"4549\"]]") as ProjectedCoordinateSystem;


            transCord = new double[] { transCord[0], transCord[1] };
            CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory();
            ProjNet.CoordinateSystems.Transformations.ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(fromCS, toCS);

            var xy = trans.MathTransform.Transform(transCord);
            return xy;



        }
        public static double[] Transferd_4_Parameter(double x_put, double y_put, double k, double red, double dx, double dy)
        {
            var x_out = y_put * k * Math.Sin(red / 180 * Math.PI) + x_put * k * Math.Cos(red / 180 * Math.PI) + dy;
            var y_out = y_put * k * Math.Cos(red / 180 * Math.PI) - x_put * k * Math.Sin(red / 180 * Math.PI) + dx;

            return new double[] { x_out, y_out };
        }

        //四参数 反算
        public static double[] ReverseTransfer_4_Parameter(double X, double Y, double scaleChange, double rotationAngle, double moveX, double moveY)
        {
            var newLon = X * scaleChange * Math.Cos(rotationAngle) - Y * scaleChange * Math.Sin(rotationAngle) + moveX;
            var newLat = X * scaleChange * Math.Sin(rotationAngle) + Y * scaleChange * Math.Cos(rotationAngle) + moveY;
            return new double[] { newLon, newLat };

        }
    }
}
